package com.example.app;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.app.retrofit.api.ApiClient;
import com.example.app.retrofit.bean.RegisterResultBean;
import com.example.app.retrofit.bean.RegisterSendBean;
import com.example.app.util.AuthUtil;
import com.example.app.util.ToastUtil;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RegisterActivity extends AppCompatActivity {

    private static final String TAG = "RegisterTestActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        EditText usernameET=findViewById(R.id.username);
        EditText passwordET=findViewById(R.id.password);
        EditText nicknameET=findViewById(R.id.nickname);
        EditText emailET=findViewById(R.id.email);
        EditText phonenumberET=findViewById(R.id.phonenumber);
        EditText classIdET=findViewById(R.id.classesId);

        Button  register=findViewById(R.id.register);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RadioGroup sexGroup=findViewById(R.id.gender);
                RadioButton sexButton=findViewById(sexGroup.getCheckedRadioButtonId());
               // String username = "32209274";
                String username = usernameET.getText().toString();
//                String password = "123456";
                String password = passwordET.getText().toString();
                String nickname = nicknameET.getText().toString();
                String classid = classIdET.getText().toString();
                String email = emailET.getText().toString();
                String phonenumber = phonenumberET.getText().toString();
                String sex = sexButton.getTag().toString();
                RegisterSendBean registerSendBean = new RegisterSendBean();
                registerSendBean.setUserName(username);
                registerSendBean.setPassword(password);
                registerSendBean.setNickName(nickname);
                registerSendBean.setClassesId(classid);
                registerSendBean.setEmail(email);
                registerSendBean.setPhonenumber(phonenumber);
                registerSendBean.setSex(sex);
                //String nickname = "韩宇";
               // registerSendBean.setNickName(nickname);
                //String classid = "2";
               // registerSendBean.setClassesId(classid);
                //String email = "163.com";
               // registerSendBean.setEmail(email);
                Call<RegisterResultBean> call = ApiClient.getInstance(getApplicationContext()).
                        getReturnData(AuthUtil.GetAuth(),registerSendBean);
                call.enqueue(new Callback<RegisterResultBean>() {
                    @Override
                    public void onResponse(Call<RegisterResultBean> call, Response<RegisterResultBean> response) {
                        if (response.body()!=null){
                            if(response.body().getCode()==200){
                                Log.d(TAG,response.body().getMsg().toString());
                                ToastUtil.ShowToast(getApplicationContext(),"注册成功");
                                finish();
                            }else{
                                ToastUtil.ShowToast(getApplicationContext(),response.body().getMsg().toString());
                            }
                        }else {
                            ToastUtil.ShowToast(getApplicationContext(),"网络连接错误");
                        }
                    }

                    @Override
                    public void onFailure(Call<RegisterResultBean> call, Throwable t) {
                        ToastUtil.ShowToast(getApplicationContext(),"网络请求错误");
                    }
                });

            }
        });
    }
}

